package util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class LineReaderTask implements Runnable {

    private String filepath;
    private long startLine;
    private long endLine;
    private CountDownLatch countDownLatch;

    public LineReaderTask(String filePath, long startLine, long endLine, CountDownLatch countDownLatch) {
        this.filepath = filePath;
        this.startLine = startLine;
        this.endLine = endLine;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        try(BufferedReader bufferedReader = new BufferedReader(new FileReader(filepath))){

            String line = "";

            long currentLine = 0;

            System.out.println(startLine + "---" + endLine);

//            while(currentLine >= startLine && currentLine <= endLine && (line = bufferedReader.readLine()) != null){
////                System.out.println(line);
//                KafkaBean.sendMessage("test_send2", line);
//                currentLine ++;
//            }

//            while(currentLine <= endLine && (line = bufferedReader.readLine()) != null){
////                System.out.println(line);
//                if(currentLine >= startLine){
//                    KafkaBean.sendMessage("test_send3", line);
//                }
//                currentLine ++;
//            }

            while(currentLine <= endLine && (line = bufferedReader.readLine()) != null){
//                System.out.println(line);
                if(currentLine >= startLine){
                    KafkaBean.sendMessage("test_send5", line);
                }
                currentLine ++;
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            countDownLatch.countDown();
        }
    }




}
